package com.gengzp.mq.server.controller;

import com.gengzp.mq.server.model.generic.MqResult;
import com.gengzp.mq.server.model.topic.req.MqCreateTopicReq;
import com.gengzp.mq.server.model.topic.req.MqEditTopicReq;
import com.gengzp.mq.server.model.topic.req.MqRemoveTopicReq;
import com.gengzp.mq.server.model.topic.resp.MqListTopicResp;
import com.gengzp.mq.server.service.MqTopicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @ClassName MqTopicController
 * @Description 消息队列topic主题控制层
 * @Author gengzp
 * @Date 2025/10/22 21:28
 */
@Controller
@RequestMapping("/mq/topic")
public class MqTopicController {

    @Autowired
    private MqTopicService mqTopicService;

    /**
     * 创建 topic
     */
    @ResponseBody
    @PostMapping("/create")
    public MqResult<Void> createTopic(@RequestBody MqCreateTopicReq req) {
        if (req.getTopic() == null || req.getTopic().isBlank()) {
            return MqResult.fail("topic 不能为空");
        }

        mqTopicService.createTopic(req);
        return MqResult.success();
    }

    /**
     * 编辑 topic 描述
     */
    @ResponseBody
    @PostMapping("/edit_description")
    public MqResult<Void> editTopicDescription(@RequestBody MqEditTopicReq req) {
        if (req.getTopic() == null || req.getTopic().isBlank()) {
            return MqResult.fail("topic 不能为空");
        }

        mqTopicService.editTopicDescription(req);
        return MqResult.success();
    }

    /**
     * 删除 topic
     */
    @ResponseBody
    @PostMapping("/remove")
    public MqResult<Void> removeTopic(@RequestBody MqRemoveTopicReq req) {
        if (req.getTopic() == null || req.getTopic().isBlank()) {
            return MqResult.fail("topic 不能为空");
        }

        mqTopicService.removeTopic(req.getTopic());
        return MqResult.success();
    }

    /**
     * 查询主题列表
     */
    @ResponseBody
    @PostMapping("/list")
    public MqResult<List<MqListTopicResp>> listTopic() {
        return MqResult.success(mqTopicService.list());
    }

}
